package io.renren.modules.app.controller;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.app.entity.AgentEntity;
import io.renren.modules.app.service.AgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Auther zxl
 * @Date 2018/12/16 10:10
 * @Description 代理商
 **/
@RestController
@RequestMapping("/app/check")
public class AppAgentController {

    @Autowired
    private AgentService agentService;


    /**
     * 列表
     * @param params
     * @return
     */
    @GetMapping("/agent/list")
    public R list(@RequestParam Map<String, Object> params) {

        PageUtils page = agentService.queryPage(params);

        return R.ok().put("page",page);

    }

    /**
     * 查询代理商信息
     * @param agentName
     * @param province
     * @param city
     * @return
     */
    @GetMapping("agent/all")
    public R all(@RequestParam(required = false,defaultValue = "",value = "agentName") String agentName,
                 @RequestParam(required = false,defaultValue = "",value = "c") String province,
                 @RequestParam(required = false,defaultValue = "",value = "city") String city){
        List<AgentEntity> list = agentService.selectAll(agentName,province,city);
        return R.ok().put("data",list);
    }

    /**
     * 保存
     * @param agentEntity
     * @return
     */
    @PostMapping("/agent/save")
    public R save(@RequestBody AgentEntity agentEntity){

        ValidatorUtils.validateEntity(agentEntity, AddGroup.class);

        agentService.addOneAgent(agentEntity);

        return R.ok("添加成功");
    }

    /**
     * 更新
     * @param agentEntity
     * @return
     */
    @PostMapping("/agent/update")
    public R update(@RequestBody AgentEntity agentEntity){

        ValidatorUtils.validateEntity(agentEntity, AddGroup.class);
        agentService.update(agentEntity);

        return R.ok("更新成功");
    }

    /**
     * 查询
     * @param agentId
     * @return
     */
    @GetMapping("/agent/get/{agentId}")
    public R selectByName(@PathVariable String agentId){

        AgentEntity agentEntity = agentService.queryById(Integer.parseInt(agentId));

        return R.ok().put("agent",agentEntity);

    }
}
